package my.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import af.spring.AfRestData;
import my.db.ClazzCourse;
import my.db.Course;
import my.db.Teacher;
import my.db.TerCrs;
import my.tool.MybatisApi;

@Controller
public class CourseController {

	@GetMapping("course")
	public String CoursePage(Model model,HttpServletRequest request) {
		
		List<Course> CourseList = MybatisApi.selectList("zhy.sesCourse.getCourses", Course.class);
		List<Map<String, Object>> TeacherList = MybatisApi.selectList("zhy.sesCourse.getTeachers",0);
		List<Map<String, Object>> TCList = MybatisApi.selectList("zhy.sesCourse.getTeacherCourse",0);
		List<Map<String, Object>> ClazzListF = MybatisApi.selectList("zhy.sesCourse.getClazzMoreF",0);
		List<Map<String, Object>> ClazzListT = MybatisApi.selectList("zhy.sesCourse.getClazzMoreT",0);
		List<Map<String, Object>> CTCList = MybatisApi.selectList("zhy.sesCourse.getClazzCourseTeacher",0);
		
		model.addAttribute("CourseList", CourseList);
		model.addAttribute("TeacherList", TeacherList);
		model.addAttribute("TCList", TCList);
		model.addAttribute("ClazzListF", ClazzListF);
		model.addAttribute("ClazzListT", ClazzListT);
		model.addAttribute("CTCList", CTCList);
		model.addAttribute("datas", IndexController.SimpleJS(request));
		return "Course_Manager";
	}
	
	//保存课程
	@PostMapping("course_saveCourse.do")
	public Object saveCourse(@RequestBody JSONObject jreq) {
		
		Course course = new Course();
		course.setCourseName(jreq.getString("CourseName"));
		course.setCourseId(jreq.getString("CourseId"));
		MybatisApi.insert("zhy.sesCourse.saveCourse", course);
		
		return new AfRestData("");
	}
	
	//保存老师的排课信息
	@PostMapping("course_saveTeachCourse.do")
	public Object saveTeachCourse(@RequestBody JSONObject jreq) {
		
		JSONArray array = jreq.getJSONArray("ids");
		TerCrs tc = new TerCrs();
		for(int i=0;i<array.size();i++) {
			tc.setId(array.getInteger(i));
			tc.setCourse_id(jreq.getInteger("course_id"));
			
			MybatisApi.update("zhy.sesCourse.updateTer_crs", tc);
		}
		
		return new AfRestData("");
	}
	
	//保存班级的排课信息
		@PostMapping("course_saveClazzCourse.do")
		public Object saveClazzCourse(@RequestBody JSONArray jreqs) {
			
			ClazzCourse CC = new ClazzCourse();
			for(int i=0;i<jreqs.size();i++) {
				JSONObject jreq = jreqs.getJSONObject(i);
				
				int clazz_id = jreq.getInteger("clazz_id");
				boolean isCourse = jreq.getBoolean("isCourse");
				System.out.println("哈哈"+isCourse);
				if(isCourse) {
					MybatisApi.delete("zhy.sesCourse.deleteTimetable", clazz_id);		
				}
				JSONArray ter_crs_ids = jreq.getJSONArray("ter_crs_ids");
				
				for(int j=0;j<ter_crs_ids.size();j++) {
					CC.setClazz_id(clazz_id);
					CC.setTer_crs_id(ter_crs_ids.getInteger(j));
					
					MybatisApi.insert("zhy.sesCourse.saveClazzCourse", CC);
				}
				MybatisApi.update("zhy.sesCourse.updateClazz", clazz_id);
			}

			
			return new AfRestData("");
		}
	
	
	//删除课程
		@PostMapping("course_deleteCourse.do")
		public Object deleteCourse(@RequestBody JSONArray jreqs) {
			
			for (int i = 0; i < jreqs.size(); i++) {
				
				MybatisApi.delete("zhy.sesCourse.deleteCourse", jreqs.get(i));
			}
			
			return new AfRestData("");
		}
		
		//删除课表
		@PostMapping("course_deleteTimetable.do")
		public Object deleteTimetable(@RequestBody Integer clazz_id) {
			
				
			MybatisApi.delete("zhy.sesCourse.deleteTimetable", clazz_id);
			
			return new AfRestData("");
		}
	
	//保存修改后的课程
	@PostMapping("course_saveUpdateCourse.do")
	public Object saveUpdateCourse(@RequestBody JSONArray jreqs) {
		
		for (int i = 0; i < jreqs.size(); i++) {
			MybatisApi.update("zhy.sesCourse.updateCourse", jreqs.getObject(i, Course.class));
		}
		return new AfRestData("");
	}
	
	//保存修改后的课表信息
		@PostMapping("course_saveUpdateTimetable.do")
		public Object saveUpdateTimetable(@RequestBody JSONArray jreqs) {

			for(int i=0;i<jreqs.size();i++) {
				
				MybatisApi.update("zhy.sesCourse.updateTimetable",jreqs.getObject(i,ClazzCourse.class));
			}
			
			return new AfRestData("");
		}
	
	//查询课表详情
	@PostMapping("course_selectTimetable.do")
	public Object selectTimetable(@RequestBody Integer jreq) {
		System.out.println("哈哈"+jreq);
		List<Map<String, Object>> Timetable = MybatisApi.selectList("zhy.sesCourse.getTimetable", jreq);
		JSONArray s = new JSONArray();
		return new AfRestData(Timetable);
	}
}
